﻿var shnt = shnt || { };

(function () {
    this.processCode = function (code) {
        if (!RTE.Cursor.s_range.isEmpty()) {
            var temp = document.createElement('div');
            if (shnt.isOutsideElement() == true) {
                var range = RTE.Cursor.get_range().$x_0;
                if (range == undefined) {
                    range = RTE.Cursor.s_range.$1d_0();
                }
                
                if (range == undefined) {
                    shnt.showDialog(390, 'error', 'This rich rext control does not support');
                }

                if ((range.get_innerHTML().match(/<pre[^>]*>/g) != null) || (range.get_innerHTML().match(/<\/pre>/g) != null)) {
                    shnt.showDialog(370, 'error', 'This operation cannot be performed');
                    return;
                }

                var pre = document.createElement('pre');
                pre.setAttribute('class', String.format('brush: {0}', code));
                pre.innerHTML = range.get_innerHTML().replace(/<p[^>]*>/g, '').replace(/<\/p>/g, '<br />');

                temp.appendChild(pre);

                RTE.Cursor.s_range.replaceWithChildren(temp);
            } else {
                var container = RTE.Cursor.s_range.parentElement();
                var val = container.getAttribute('class');
                if (val.indexOf(code) > -1) {
                    var childs = container.childNodes;

                    var first = container.cloneNode();
                    var second = container.cloneNode();
                    first.innerHTML = second.innerHTML = '';

                    var markers = RTE.Cursor.s_range.getMarkers();
                    var inner = RTE.Cursor.s_range.getInnerNodes();

                    var isFirst = true;
                    for (var i = 0; i < childs.length; ++i) {
                        if (markers.indexOf(childs[i]) > -1) {
                            isFirst = false;
                            continue;
                        }
                        if (inner.indexOf(childs[i]) > -1) {
                            isFirst = false;
                            temp.appendChild(childs[i].cloneNode());
                            continue;
                        }
                        if (isFirst) {
                            first.appendChild(childs[i].cloneNode());
                        } else {
                            second.appendChild(childs[i].cloneNode());
                        }
                    }

                    if (first.childNodes.length > 0) SP.UI.UIUtility.insertBefore(first.cloneNode(), container);
                    if (temp.childNodes.length > 0) SP.UI.UIUtility.insertBefore(temp.cloneNode(), container);
                    if (second.childNodes.length > 0) SP.UI.UIUtility.insertBefore(second.cloneNode(), container);
                    container.remove();
                }
                else {
                    var match = val.match(/brush\: (.*)/);
                    if (match != null) {
                        shnt.showDialog(470, 'info', String.format('For this code block you have to use "{0}" action', match[1]));
                    }

                }
            }
        }
    };
    this.isOutsideElement = function() {
        var parents = RTE.Cursor.s_range.getParentChain();
        for (var i = 0; i < parents.length; ++i) {
            if ((parents[i].tagName != undefined)&&(parents[i].tagName.toLowerCase() == 'pre')) {
                return false;
            }
        }
        return true;
    };
    this.showDialog = function (width, type, text) {
        var pic = type == 'info' ? 'mewa_infob.gif' : 'erroricon.png';

        var diaolgContent = document.createElement('div');
        diaolgContent.innerHTML = String.format('<table style="vertical-align:middle"><tr><td><img src="{0}" style="margin-top: 10px;"></img></td><td><span style="font-size: 19px;font-weight: bold;">{1}</span></td></tr></table>', SP.Utilities.Utility.getImageUrl(pic), text);

        var options = SP.UI.$create_DialogOptions();
        options.html = diaolgContent;
        options.title = ' ';
        options.allowMaximize = false;
        options.showClose = true;
        options.width = width;
        options.height = 70;
        SP.UI.ModalDialog.showModalDialog(options);
    };
    this.applyFormating = function() {
        SyntaxHighlighter.highlight();
    };
}).apply(shnt);

SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();

window.addEventListener("hashchange", shnt.applyFormating, false);